<%
'#################################################################################
'## UnWritten Open Source Blog Engine
'#################################################################################
'## Copyright (C) 2008 Luigi Violin
'##
'## This program is free software: you can redistribute it and/or modify
'## it under the terms of the GNU General Public License as published by
'## the Free Software Foundation, either version 2 of the License, or
'## (at your option) any later version.
'##
'## This program is distributed in the hope that it will be useful,
'## but WITHOUT ANY WARRANTY; without even the implied warranty of
'## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'## GNU General Public License for more details.
'##
'## You should have received a copy of the GNU General Public License
'## along with this program.  If not, see <http://www.gnu.org/licenses/>.
'##
'## You can obtain support from our forums at:
'##     <http://www.unwrittenblog.com/forums/>
'## Contact the author directly:
'##     <zaamit@hotmail.com/>
'##
'#################################################################################
%>
<!--#include virtual="/includes/core.asp"-->
<%
if request.form("action") = 1 then
'insert new
Dim newname
newname = getname(request.form("title"))
strsql = "insert into " & db_prefix & "posts(author,pdate,pdate_gmt,title,content,excerpt,ent_name,modified,modified_gmt,icon,views,publish_status,ratingsum,ent_type) " 
strsql = strsql & "values(" & Session("u_id") & ",'" & str2dat(now()) & "','" & str2dat(DateAdd("n", Application("GMToffset"), now())) & "','" & replace(request.form("title"),"'","''") & "','" & replace(request.form("content"),"'","''") & "','" & replace(getexc(request.form("content")),"'","''") & "','" & newname & "','" & str2dat(now()) & "','" & str2dat(DateAdd("n", Application("GMToffset"), now())) & "','" & request.form("icon") & "',0," & request.form("publish_status") & ",0," & request.form("type")
strsql = strsql & ")"
'response.write strsql
dbConn.execute strsql
dbRS2.open "select id from " & db_prefix & "posts where ent_name = '" & newname & "'",dbConn
'manage tags
updateTags dbRS2("id"),request.form("tags")
dbRS2.close
'redirect back
if request.form("type") = 1 then
	Response.Redirect "post-list.asp"
	else
		Response.Redirect "article-list.asp"
end if
else
'update existing
strsql = "update " & db_prefix & "posts set "
strsql = strsql & " title = '" & replace(request.form("title"),"'","''") & "', "
strsql = strsql & " content = '" & replace(request.form("content"),"'","''") & "', "
strsql = strsql & " excerpt = '" & getexc(replace(request.form("content"),"'","''")) & "', "
strsql = strsql & " icon = '" & request.form("icon") & "', "
strsql = strsql & " modified = '" & str2dat(now()) & "', "
strsql = strsql & " modified_gmt = '" & str2dat(DateAdd("n", Application("GMToffset"), now())) & "', "
strsql = strsql & " publish_status = " & request.form("publish_status") & ", "
strsql = strsql & " ent_type = " & request.form("type")
strsql = strsql & " where id = " & request.form("id")
updateTags request.form("id"),request.form("tags")
'response.Write strsql
dbConn.execute strsql
if request.form("type") = 1 then
	Response.Redirect "post-list.asp"
	else
		Response.Redirect "article-list.asp"
end if
end if

function updateTags(pid,tagstr)
  if request.Form("type") = 1 then
    'blog post tags
	dbConn.execute "delete from " & db_prefix & "posttags where postid = " & pid
	tagsplit = split(tagstr,",")
	for i = 0 to ubound(tagsplit,1)
		curtag = trim(tagsplit(i))
		dbConn.execute "insert into " & db_prefix & "posttags(postid,tagid) values(" & pid & "," & gettagid(curtag) & ")"
	next
	else
	  'article category
	  dbConn.execute "update " & db_prefix & "posts set category = '" & tagstr & "' where id = " & pid
  end if
end function
%>